package com.leizi.igym.util;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSON;
import com.leizi.igym.properties.JWTProperties;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-26  11:47
 * @Description:
 */

public class RedisUtils {

    private RedisUtils() {
    }

    private static RedisTemplate<String, String> redisTemplate = SpringUtil.getBean("redisTemplate",
            RedisTemplate.class);


    public static void set(String key,Object o){
        redisTemplate.opsForValue().set(key, JSON.toJSONString(o));
    }

    public static void set(String key,Object o,Long expireTime){
        redisTemplate.opsForValue().set(key, JSON.toJSONString(o),expireTime , TimeUnit.MILLISECONDS);
    }

    public static <T> T get(String key,Class<T> clazz){
        String jsonStr = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(jsonStr)){
            return null;
        }
        return JSON.parseObject(jsonStr,clazz);
    }

    public static boolean remove(String key){
        return redisTemplate.delete(key);
    }

    public static boolean containKey(String key){
        return redisTemplate.hasKey(key);
    }


    public static boolean add(String key,String ...str){
        return redisTemplate.opsForSet().add(key, str) == 1;
    }

    public static boolean sremove(String key,Object ...o){
        return redisTemplate.opsForSet().remove(key,o) == 1;
    }

    public static Long size(String key){
        return redisTemplate.opsForSet().size(key);
    }

    public static boolean containValue(String key,Object value){
        return redisTemplate.opsForSet().isMember(key,value);
    }

    public static Set<String> keys(String pattern){
        return redisTemplate.keys(pattern);

    }
}
